package com.suishoupai.xmb.controller;

import com.github.pagehelper.Page;
import com.suishoupai.xmb.entity.AjaxMessage;
import com.suishoupai.xmb.exception.XmbException;
import com.suishoupai.xmb.service.EvaluateService;
import com.suishoupai.xmb.utils.PageUtil;
import com.suishoupai.xmb.utils.SecurityUserUtil;
import com.suishoupai.xmb.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by caojian on 2017/4/26.
 */
@Controller
@RequestMapping("/sale/evaluate")
@PreAuthorize("hasRole('ROLE_SALE')")
public class SaleEvaluateController {
    @Autowired
    private EvaluateService evaluateService;

    @GetMapping("")
    public Object index(@RequestParam Map map, Model model) {
        long saleId = SecurityUserUtil.getUser().getId();
        PageUtil.startPage(map);
        Page page = (Page) evaluateService.getEvaluateListBySaleId(saleId);
        model.addAttribute("evaluateList", page.toPageInfo());
        return "sale-evaluate-list";
    }

    @PostMapping("/{evaluateId}/reply")
    @ResponseBody
    public Object reply(@PathVariable("evaluateId") long evaluateId, String reply) {
        ValidateUtil.notNull(reply);
        long saleId = SecurityUserUtil.getUser().getId();
        try {
            evaluateService.reply(evaluateId, saleId, reply);
        } catch (XmbException e) {
            return AjaxMessage.newErrorHintMsg(e.getError());
        }
        return AjaxMessage.newSuccessMsg();
    }
}
